package cn.hnu.jpa.jpaExample.ManyToMany;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class MMSingleCustomer implements Serializable {

	/**
	 * 多对多单向 表关联
	 */
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	
	private String name;
	
	private List<MMSingleAddr> addrs = new ArrayList<MMSingleAddr>();

	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany (cascade = {CascadeType.ALL}) //产生的manytomany表没有主键
	@JoinTable(
			name="manytomany",
			joinColumns = {@JoinColumn(name="cus_id",referencedColumnName = "id")},
			inverseJoinColumns = {@JoinColumn(name="addr_id",referencedColumnName = "id")}
			)
	public List<MMSingleAddr> getAddrs() {
		return addrs;
	}

	public void setAddrs(List<MMSingleAddr> addrs) {
		this.addrs = addrs;
	}
	
	public void addAddr(MMSingleAddr addr){
		this.addrs.add(addr);
	}
}
